//
//  OO_PayViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/30.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_PayViewController.h"

@interface OO_PayViewController ()
@property (weak, nonatomic) IBOutlet UIButton *zhiFuBaoButton;
@property (weak, nonatomic) IBOutlet UIButton *huoDaoFuKuanButton;
@property (weak, nonatomic) IBOutlet UILabel *payMoneyLable;
@property (nonatomic,assign) NSInteger payType;
@end

@implementation OO_PayViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"收银台";
    
    UIBarButtonItem *leftItem  = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:0 target:self action:@selector(cancelButtonPress)];
    leftItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.payMoneyLable.text = [NSString stringWithFormat:@"待支付：¥%.2lf",self.allMoney];
}
-(void)cancelButtonPress{
    WeakSelf
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要取消支付么？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
    UIAlertAction * cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:true completion:nil];
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)zhifubaoPay:(UIButton *)sender {
    self.zhiFuBaoButton.selected = YES;
    self.huoDaoFuKuanButton.selected = NO;
    
    self.payType = 1;
    
    
    
}
- (IBAction)huodaofukuanPay:(id)sender {
    self.zhiFuBaoButton.selected = NO;
    self.huoDaoFuKuanButton.selected = YES;
    self.payType = 2;
}

- (IBAction)sureButtonPress:(UIButton *)sender {
    
    if (self.payType == 0) {
        [MBProgressHUD  showTipMessageInWindow:@"请选择支付方式"];
        return;
        
    }
    
    if (self.payType == 1) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该商品暂时不支持支付宝支付,请选择其他支付方式" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
           
        }];
        [alert addAction:okAction];
       
        [self presentViewController:alert animated:true completion:nil];
        
    
    }
    else{
        WeakSelf
        [MBProgressHUD showActivityMessageInView:@"请稍等..."];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            JQFMDB *db = [JQFMDB shareDatabase];
            
            
            
            
            
            
            NSDate *date=[NSDate date];
            NSDateFormatter *format1=[[NSDateFormatter alloc] init];
            [format1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
            NSString *dateStr;
            dateStr=[format1 stringFromDate:date];
            
            NSMutableArray *ARR = [NSMutableArray new];
            for (OO_OrderModel *model in self.orderArr) {
                
                model.time = dateStr;
                [ARR addObject:model];
                [db jq_deleteTable:TOBEPAY whereFormat:@"where ID = '%@'",model.ID];
            }
            
            
            [db jq_insertTable:WAITSEND dicOrModelArray:ARR];
            
       
            
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                [MBProgressHUD showTipMessageInWindow:@"订单已提交,请前往我的订单查看"];
                [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
                
            });
            
            
        });
        
        
        
    }
    
    
}

@end
